package ticketdao

import (
	"fmt"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/models/ticket"
	"yundian/internel/app/models/ticketmodel"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
)

func (d *TicketDao) GetTicketLineOrdersList(param *request.GetTicketLineOrdersListReq) *dingo.ErrorWrap {
	var (
		lst    []*request.GetTicketLineOrdersListRes
		count  int64
		limit  int = param.PageSize
		offset int = param.PageSize * (param.PageNo - 1)
	)

	fields := `tlo.*,tl.line_name,DATE_FORMAT(tlo.create_time,'%Y-%m-%d %T')create_time,
	DATE_FORMAT(tlo.pay_time,'%Y-%m-%d %T')pay_time,DATE_FORMAT(tlo.update_time,'%Y-%m-%d %T')update_time`
	db := global.Orm.Table(fmt.Sprintf(`%s tlo`, ticket.NewOrderModel().TableName())).Select(fields)
	db.Joins(fmt.Sprintf(`LEFT JOIN %s tl ON tlo.ticket_id=tl.id`, ticketLine))
	db.Where("tlo.member_id=?", param.MemberId)

	if param.Status > 0 {
		db.Where("tlo.status=?", param.Status)
	}

	db.Order("tlo.id DESC")
	db.Limit(limit).Offset(offset).Find(&lst).Offset(-1).Limit(-1).Count(&count)
	return dingo.ErrorIf(db.Error, lst, count)
}

func (d *TicketDao) GetTicketLineList(param *request.BaseListReq) *dingo.ErrorWrap {
	var (
		lst    []*ticketmodel.TicketLineModel
		count  int64
		limit  int = param.PageSize
		offset int = param.PageSize * (param.PageNo - 1)
	)

	db := global.Orm.Table(fmt.Sprintf(`%s tl`, ticketLine))

	db.Order("id DESC")
	db.Limit(limit).Offset(offset).Find(&lst).Offset(-1).Limit(-1).Count(&count)
	return dingo.ErrorIf(db.Error, lst, count)
}

func (d *TicketDao) GetTicketLineInfo(id int) *dingo.ErrorWrap {
	tickInfo := &ticketmodel.TicketLineModel{}

	db := global.Orm.Table(fmt.Sprintf(`%s tl`, ticketLine)).Where("id = ?", id).Find(tickInfo)

	return dingo.ErrorIf(db.Error, tickInfo)
}
